package jdbc;

import java.sql.*;

public class DBUtil {

    public void connect() throws Exception {
        Class.forName("com.mysql.cj.jdbc.Driver");
    }
    public void add(User user) throws SQLException {

        Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/information?useUnicode=true&characterEncoding=UTF-8&userSSL=false&serverTimezone=GMT%2B8", "root", "123456");
        String sql = "insert into userinfo(id,username,password) values(?,?,?)";
        PreparedStatement st = conn.prepareStatement(sql);
        st.setString(1, user.getID());
        st.setString(2, user.getUsername());
        st.setString(3, user.getkey());
        Statement state=conn.createStatement();
        int count=state.executeUpdate(sql);
        System.out.println(count);
        state.close();
        conn.close();
    }
    public void delete(String name) throws SQLException {
        Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/information?useUnicode=true&characterEncoding=UTF-8&userSSL=false&serverTimezone=GMT%2B8", "root", "123456");
        String sql = "DELETE FROM 表名 WHERE ?=username";
        PreparedStatement st = conn.prepareStatement(sql);
        st.setString(1, name);
        Statement state=conn.createStatement();
        int count=state.executeUpdate(sql);
        System.out.println(count);
        state.close();
        conn.close();

    }
    public void query(String name) throws SQLException {
        Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/information?useUnicode=true&characterEncoding=UTF-8&userSSL=false&serverTimezone=GMT%2B8", "root", "123456");
        String sql = "SELECT * from userinfo where username = ?";
        PreparedStatement st = conn.prepareStatement(sql);
        st.setString(1, name);
        Statement state=conn.createStatement();
        int count=state.executeUpdate(sql);
        System.out.println(count);
        state.close();
        conn.close();
    }
    public void queryAll() throws SQLException {
        Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/information?useUnicode=true&characterEncoding=UTF-8&userSSL=false&serverTimezone=GMT%2B8", "root", "123456");
        String sql = "select * from userinfo";
        Statement state=conn.createStatement();
        int count=state.executeUpdate(sql);
        System.out.println(count);
        state.close();
        conn.close();
    }
    public void update(String name, String password) throws SQLException {
        String sql = "update userinfo set password = ? where username = ?";
        Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/information?useUnicode=true&characterEncoding=UTF-8&userSSL=false&serverTimezone=GMT%2B8", "root", "123456");
        PreparedStatement st = conn.prepareStatement(sql);
        st.setString(1, password);
        st.setString(2, name);
        Statement state=conn.createStatement();
        int count=state.executeUpdate(sql);
        System.out.println(count);
        state.close();
        conn.close();
}
}
